package com.xja.controller;

import com.xja.model.dto.ArticlePublishDTO;
import com.xja.model.entity.Article;
import com.xja.model.vo.resp.ArticleDetailVO;
import com.xja.model.vo.resp.ArticleItemVO;
import com.xja.model.vo.resp.Result;
import com.xja.service.IArticleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 文章表 前端控制器
 * </p>
 *
 * @author caijg
 
 */
@RestController
@Tag(name = "文章模块")
@RequestMapping("/user/article")
@Slf4j
@RequiredArgsConstructor
public class ArticleController {

    private final IArticleService articleService;

    @Operation(summary = "用户发布文章")
    @PostMapping("/publish")
    public Result<Void> publish(@RequestBody ArticlePublishDTO articlePublishDTO) {
        log.info("用户发布文章：{}", articlePublishDTO);
        articleService.publish(articlePublishDTO);
        return Result.success();
    }

    @Operation(summary = "用户获取文章列表")
    @GetMapping("/list")
    public Result<List<ArticleItemVO>> list(@RequestParam(name = "searchText", required = false) String searchText) {
        log.info("用户请求获取文章列表...searchText:{}", searchText);
        List<ArticleItemVO> articleItemVOList = articleService.getList(searchText);
        return Result.success(articleItemVOList);
    }

    @Operation(summary = "用户获取文章详情")
    @GetMapping("/detail/{id}")
    public Result<ArticleDetailVO> detail(@PathVariable Long id) {
        log.info("用户请求获取文章详情：{}", id);
        ArticleDetailVO articleDetailVO = articleService.getDetail(id);
        return Result.success(articleDetailVO);
    }

    @Operation(summary = "用户检查文章点赞状态")
    @GetMapping("/like/check/{articleId}")
    public Result<Boolean> checkLikeStatus(@PathVariable Long articleId) {
        log.info("用户请求检查点赞状态：{}", articleId);
        Boolean b = articleService.checkLikeStatus(articleId);
        return Result.success(b);
    }

    @Operation(summary = "用户点赞/取消点赞文章")
    @PostMapping("/like/{articleId}")
    public Result<Boolean> like(@PathVariable Long articleId) {
        log.info("用户点赞/取消点赞文章：{}", articleId);
        Boolean liked = articleService.like(articleId);
        return Result.success(liked);
    }

    @PostMapping("/views/{articleId}")
    @Operation(summary = "浏览量+1")
    public Result<Void> views(@PathVariable Long articleId) {
        log.info("用户浏览文章：{}", articleId);
        articleService.views(articleId);
        return Result.success();
    }

    @Operation(summary = "用户获取喜欢文章列表")
    @GetMapping("/my-love")
    public Result<List<Article>> getMyLoveArticle() {
        log.info("用户请求获取喜欢文章列表");
        List<Article> articleList = articleService.getMyLoveArticle();
        return Result.success(articleList);
    }

    @Operation(summary = "用户获取我的文章列表")
    @GetMapping("/my")
    public Result<List<Article>> getMyArticle() {
        log.info("获取我的文章");
        List<Article> articleList = articleService.getMyArticle();
        return Result.success(articleList);
    }

    @Operation(summary = "用户删除文章")
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody ArrayList<String> ids) {
        log.info("删除文章：{}", ids);
        Boolean deleted = articleService.delete(ids);
        return Result.success(deleted);
    }
}
